package com.gexin.model.product;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisWeiXinKeyConst;
import com.gexin.model.product.base.BaseProductSku;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 * 业务名称:  t_product_sku(SKU 商品属性表)
 */
@SuppressWarnings("serial")
public class ProductSku extends BaseProductSku<ProductSku> {
	public static final ProductSku dao = new ProductSku();

	public ProductSku getById(String id){
		return dao.findById(id);
	}

	/**
	 * 判断商品是否启用了SKU
	 * @param pruduct_id
	 * @return
	 */
	public boolean isEnableSku(String pruduct_id){
		Long count =  CacheUtils.getEhcache("sku:"+pruduct_id);
		if(count==null){
			count =Db.queryLong("select count(*) from t_product_sku_attr  where product_id=?",pruduct_id);
			CacheUtils.setEhcache("sku:"+pruduct_id, count);
		}
		return count>0?true:false;
	}

	/**
	 * 根据商品ID 查询下面的SKU 信息
	 * @param productid
	 * @return
	 */
	public List<ProductSku>  getStockByProduct(int productid){
		return find("select sku_id,stock_count,price,group_price from t_product_sku  where  product_id=?",productid);
	}

	/**
	 * 根据商品ID 查询下面的SKU 信息
	 * @param productid
	 * @return
	 */
	public List<ProductSku>  getStockByProducts(int productid){
		return find("select * from t_product_sku  where  product_id=?",productid);
	}

	/**
	 *
	 * fang  2016-06-21
	 * 商品修改  中   拿到所有的
	 * @param productid
	 * @return
	 */
	public List<ProductSku>  getSkuByProductId(int productid){
		return find("select group_price,sku_id,stock_count,price,product_id,sku_code from t_product_sku  where sku_state=1 and  product_id=?",productid);
	}

	public List<ProductSku>  getAllSkuByProductId(int productid){
		return find("select * from t_product_sku  where sku_state=1 and  product_id=?",productid);
	}

	public String  cache_getStockByProduct(int productid){
		String  json  =  CacheUtils.get(RedisWeiXinKeyConst.Cache_Products_Sku+productid);
		if(json==null){
			json  =  JsonKit.toJson(getStockByProduct(productid));
//			CacheUtils.set(RedisWeiXinKeyConst.Cache_Products_Sku+productid,json,60);
		}
		return json;
	}

	/**
	 *
	 * fang  2016-06-21
	 * 商品修改要用的  sku_json
	 *
	 */
	/*public String  getSKUByProductId_ToJson(int product_id){
		List<ProductSku>  list =getSkuByProductId(product_id);
		java.util.Map<String, String> map =new HashMap<String, String>();
		for(ProductSku  sku : list ){
			map.put("sku_id",sku.getSkuId().toString());
			map.put("stock_count", sku.getStockCount().toString());
			map.put("price", sku.getPrice().toString());
			map.put("product_id", sku.getProductId().toString());
		}
		return  JsonKit.toJson(map);
	}*/
}
